﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MyWebPagesStarterKit;
using MyWebPagesStarterKit.Controls;
using MyWebPagesStarterKit.Rewriter;

public partial class SectionControls_NewsTeaser : SectionControlBaseClass
{
    private NewsTeaser _section;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        updateViews();
    }

    private void updateViews()
    {
        if (ViewMode == ViewMode.Edit)
        {
            multiview.SetActiveView(editView);
            txtItemCount.Text = Convert.ToString(_section.ItemCount);
            populateList(SiteMap.RootNode, 0);

        }
        else
        {
            multiview.SetActiveView(readonlyView);
            if (_section.PageID != null)
            {
                //Limit rows or not
                DataView dv = _section.GetItems(_section.PageID);
                if (_section.ItemCount == 0)
                    news.DataSource = dv; //No limit
                else //Limited rows
                    news.DataSource = new MyWebPagesStarterKit.Utils.FilterRows(dv, _section.ItemCount);

                news.DataBind();
            }
        }
    }

    protected string GetHtml(string text)
    {
        text = TextFormatter.RemoveHtmlTags(text);
        if (text.Length > 70)
            return text.Substring(0, 70) + "... Läs mer &raquo;";
        else
            return text;
    }

    private void populateList(SiteMapNode parentNode, int level)
    {
        WebSite _website = WebSite.GetInstance();


        foreach (SiteMapNode node in parentNode.ChildNodes)
        {
            if (node["pageId"] != null)
            {
                string title = node.Title;
                for (int i = 0; i < level; i++)
                    title = "..." + title;

                if (node["visible"] == false.ToString())
                    title += " (Hidden)";

                if (node["hidden"] == true.ToString())
                    title += " (Hidden)";

                if (node["pageId"] == _website.HomepageId)
                    title += " (HomePage)";

                ListItem item = new ListItem(title, node["pageId"]);

                lstPages.Items.Add(item);
                populateList(node, level + 1);

                if (Convert.ToString(node["pageId"]) == _section.PageID)
                    lstPages.SelectedValue = _section.PageID;
            }
        }
    }

    protected void Save(object sender, EventArgs e)
    {
        _section.ItemCount = Convert.ToInt32(txtItemCount.Text);
        _section.PageID = lstPages.SelectedValue;
        _section.SaveData();
    }

    protected string GetPrefix(string link)
    {
        return string.Format("{0}{1}", SectionRewriter.GetPrefix("News"), link);    
    }

    public override bool HasAdminView
    {
        get { return true; }
    }

    public override string InfoUrl
    {
        get { return ""; }
    }

    public override ISection Section
    {
        get
        {
            return (NewsTeaser)_section;
        }
        set
        {
            _section = (NewsTeaser)value;
        }
    }
}
